﻿#include "newformulastandarditemmodel.h"

NewFormulaStandardItemModel::NewFormulaStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header<<"商品型号"
         <<"商品名称"
        <<"单位"
       <<"净价"
      <<"金额"
     <<"数量";
    setHorizontalHeaderLabels(header);
}

bool NewFormulaStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role){
    if(index.column() != 5 && index.column() != 3){
        return false;
    }

    //5:数量 3:净价
    int count = 0;
    double price = 0;
    Product *product = products[index.row()];
    if(index.column() == 5){
        count = value.toInt();
        price = item(index.row(), 3)->text().toDouble();
        if(count<1) count=1;
        product->count = count;
        QStandardItemModel::setData(index, count, role);
    }else{
        count = item(index.row(), 5)->text().toInt();
        price = value.toDouble();
        if(price<0) price=0;
        product->guidePrice1 = price;
        QStandardItemModel::setData(index, price, role);
    }

    price *= count;
    QStandardItemModel::setItem(index.row(), 4, new QStandardItem(QString::number(price, 'f', 2)));
    refreshTotal();

    return true;
}

QVariant NewFormulaStandardItemModel::data(const QModelIndex &index, int role) const{
    QVariant value = QStandardItemModel::data(index, role);
    //设置内容居中
    if(role==Qt::TextAlignmentRole){
        return  Qt::AlignCenter;
    }

    //设置内容颜色
    if(widgetType == WidgetType::SplitProduct){
        if (role == Qt::TextColorRole && (index.column() == 5 || index.column() == 3))
            return QVariant::fromValue(QColor(Qt::blue));
    }else{
        if (role == Qt::TextColorRole && (index.column() == 5))
            return QVariant::fromValue(QColor(Qt::blue));
    }
    return value;
}

void NewFormulaStandardItemModel::setWidgetType(WidgetType widgetType){
    this->widgetType = widgetType;
}

void NewFormulaStandardItemModel::insertData(Product &product, int defaultCount)
{
    QList<QStandardItem*> rowData;

    defaultCount = defaultCount<1?1:defaultCount;

    rowData.append(new QStandardItem(product.type));
    rowData.append(new QStandardItem(product.name));
    rowData.append(new QStandardItem(product.unit));
    rowData.append(new QStandardItem(QString::number(product.inPrice, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(product.inPrice*defaultCount, 'f', 2)));
    rowData.append(new QStandardItem(QString("%1").arg(defaultCount)));

    product.count = defaultCount;
    products.append(&product);
    insertRow(rowCount()-1, rowData);
}

void NewFormulaStandardItemModel::insertData(QList<Product*> products, int defaultCount)
{
    int i=0;

    for(Product *product:products)
    {
        if(product->checkExists(this->products)){
            i++;
        }else{
            insertData(*product, defaultCount);
        }
    }

    if(i!=0){
        Tools::showRepeatProductMsg();
    }
}

QList<Product*> NewFormulaStandardItemModel::currentProducts()
{
    return products;
}

void NewFormulaStandardItemModel::appendData(Product &product){
    QList<QStandardItem*> rowData;

    rowData.append(new QStandardItem(product.type));
    rowData.append(new QStandardItem(product.name));
    rowData.append(new QStandardItem(product.unit));
    rowData.append(new QStandardItem(QString::number(product.inPrice, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(product.inPrice*product.count, 'f', 2)));
    rowData.append(new QStandardItem(QString("%1").arg(product.count)));

    products.append(&product);
    appendRow(rowData);
}

void NewFormulaStandardItemModel::appendData(QList<Product*> products)
{
    for(Product *product:products){
        appendData(*product);
    }

    refreshTotal();
}

void NewFormulaStandardItemModel::reset()
{
    clear();
    products.clear();
    setHorizontalHeaderLabels(header);
}

void NewFormulaStandardItemModel::refreshTotal(){
    double total = 0;

    for(int i=0; i< rowCount();i++){
        QStandardItem *curItem = item(i, 4);
        if(curItem == nullptr){
            break;
        }
        double price = curItem->text().toDouble();
        total += price;
    }

    emit updateTotal(total);
}

QString NewFormulaStandardItemModel::getAllInPrices(){
    QString inPrices;

    for(int i=0; i< rowCount();i++){
        QStandardItem *curItem = item(i, 3);
        if(curItem == nullptr){
            break;
        }
        inPrices.append(curItem->text()+"#");
    }

    return inPrices;
}
